package xyz.scootaloo.draft.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author flutterdash@qq.com
 * @since 2021/5/12 9:11
 */
public class Server {
    static final int port = 8888;

    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(port);
            System.out.println("启动服务器...");
            Socket s = server.accept();
            System.out.println("客户端:" + s.getInetAddress().getHostAddress() + "已连接服务器");

            InputStream in;
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            // 读取客户端发来的消息
            String msg = br.readLine();
            System.out.println("客户端:" + msg);

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bw.write(msg + "\n");
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
